package com.lykj.llw.box.ui.activity

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import app.dinus.com.itemdecoration.LinearDividerItemDecoration
import com.blankj.utilcode.util.GsonUtils
import com.gyf.immersionbar.BarHide
import com.gyf.immersionbar.ImmersionBar
import com.lykj.lib_base.base.BaseActivity
import com.lykj.lib_base.internal.extfun.clickWithTrigger
import com.lykj.lib_base.internal.utils.ToastUtils
import com.lykj.lib_base.net.http.CustomResponseBodyObserver
import com.lykj.lib_base.net.http.RetrofitManager
import com.lykj.llw.box.R
import com.lykj.llw.box.adapter.PlayingAdapter
import com.lykj.llw.box.data.bean.response.PlayingListResponse
import com.lykj.llw.box.databinding.ActPlayedGameBinding
import io.reactivex.disposables.Disposable
import okhttp3.ResponseBody

/**
 * ================================================
 * 玩过的游戏
 * ================================================
 *
 * @author gosha
 * @date 2023/12/1-11:37
 */
class PlayedGameActivity : BaseActivity() {
    lateinit var binding: ActPlayedGameBinding

    private val mPlayingAdapter = PlayingAdapter()
    private val mPlayingData = ArrayList<PlayingListResponse.DataDTO>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActPlayedGameBinding.inflate(layoutInflater)
        setContentView(binding.root)
        initUI()
        initData()
        setListener()
    }

    override fun initUI() {
        binding.titleBar.tvTitle.text = "玩过的游戏"
        binding.titleBar.tvRightOption.visibility = View.GONE

        initPlayingRCV()
    }

    private fun initPlayingRCV() {
        val linearLayoutManager = LinearLayoutManager(this)
        linearLayoutManager.orientation = LinearLayoutManager.VERTICAL
        binding.rcvGame.layoutManager = linearLayoutManager
        binding.rcvGame.setHasFixedSize(true)

        val dividerItemDecoration = LinearDividerItemDecoration(
            this, LinearDividerItemDecoration.LINEAR_DIVIDER_VERTICAL
        )
        dividerItemDecoration.setDivider(
            resources.getDrawable(
                R.drawable.divider_10dp, resources.newTheme()
            )
        )
        binding.rcvGame.addItemDecoration(dividerItemDecoration)
        mPlayingAdapter.data = mPlayingData
        val emptyView = View.inflate(this, R.layout.empty_playing, null)
        mPlayingAdapter.setEmptyView(emptyView)
        mPlayingAdapter.setOnItemClickListener { adapter, view, position ->
            val intent = Intent(this, GameDetailActivity::class.java)
            intent.putExtra("gid", mPlayingData[position].gid)
            startActivity(intent)
        }
        binding.rcvGame.adapter = mPlayingAdapter
    }

    //获取正在玩的游戏列表
    private fun obtainPlayingGames() {
        clearBodyParam()
        initBodyParam()
        RetrofitManager.instance.obtainPlayedGames(bodyParams,
            object : CustomResponseBodyObserver<ResponseBody>() {
                override fun onSubscribe(d: Disposable) {
                    addDisposables(this@PlayedGameActivity, d)
                }

                override fun specificHandle(responseBody: String) {
                    val bean = GsonUtils.fromJson(responseBody, PlayingListResponse::class.java)

                    mPlayingData.clear()
                    if (!bean.data.isNullOrEmpty()) {
                        mPlayingData.addAll(bean.data)
                    }

                    mPlayingAdapter.notifyDataSetChanged()
                }

                override fun handleException(ret: String, msg: String?) {
                    msg?.let {
                        ToastUtils.showShort(this@PlayedGameActivity, it)
                    }
                }
            })
    }

    override fun initData() {
        obtainPlayingGames()
    }

    override fun onResume() {
        super.onResume()

        ImmersionBar.with(this)
            .statusBarDarkFont(true) //状态栏字体是深色，不写默认为亮色
            .navigationBarDarkIcon(true)
            .hideBar(BarHide.FLAG_HIDE_NAVIGATION_BAR)
            .navigationBarEnable(false)
            .init()
    }

    override fun setListener() {
        binding.titleBar.ivBack.clickWithTrigger {
            finish()
        }

        binding.srfGame.setOnRefreshListener {
            obtainPlayingGames()
            it.finishRefresh(300)
        }
    }

    override fun onDestroy() {
        removeDisposables(this)
        super.onDestroy()
    }
}